﻿using Excel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace ExcelToJson
{
    public class ExcelClass
    {
        public string className;
        public List<string> varNames = new List<string>();
        public List<string> typeNames = new List<string>();
        public List<string> valueNames = new List<string>();
        private DataTable dataTable;
        public ExcelClass(DataTable dataTable)
        {
            if (dataTable.Rows.Count < 3)
            {
                Form1.logInfo(dataTable.TableName + "少于3行");
            }
            this.dataTable = dataTable;
            className = dataTable.TableName;
        }
        public string ExportJson()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            for (int i = 3; i < dataTable.Rows.Count; i++)
            {
                string classContent = getJsonClassContent(i);
                sb.Append(classContent);
                if (i != dataTable.Rows.Count-1)
                {
                    sb.AppendLine(",");
                }
            }
            sb.AppendLine("]");
            return sb.ToString();

        }
        public string ExportJsonInTableName()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("\""+className+"\"" + ":");
            sb.AppendLine("[");
            for (int i = 3; i < dataTable.Rows.Count; i++)
            {
                string classContent = getJsonClassContent(i);
                sb.Append(classContent);
                if (i != dataTable.Rows.Count - 1)
                {
                    sb.AppendLine(",");
                }
            }
            sb.AppendLine("]");
            return sb.ToString();
        }
        private string getJsonClassContent(int rowIndex)
        {
            int count = dataTable.Columns.Count;
            int startCount = 0;
            StringBuilder sb = new StringBuilder();
            if (dataTable.Rows[0][0].ToString()=="Id")
            {
                sb.Append("\"");
                string varContent = getJsonVarContent(dataTable.Rows[0][0].ToString(), dataTable.Rows[1][0].ToString(), dataTable.Rows[rowIndex][0].ToString());
                sb.Append(varContent);
                sb.AppendLine("\""+":");
                startCount = 1;
            }
            sb.AppendLine("{");
            for (int i = startCount; i < count; i++)
            {
                string varContent = getJsonVarContent(dataTable.Rows[0][i].ToString(), dataTable.Rows[1][i].ToString(), dataTable.Rows[rowIndex][i].ToString());
                sb.Append(varContent);
                if (i != count - 1)
                {
                    sb.AppendLine(",");
                }
                else
                {
                    sb.AppendLine();
                }
            }
            sb.AppendLine("}");
            return sb.ToString();
        }
        private string getJsonVarContent(string varName, string typeName, string content)
        {
            StringBuilder varContent = new StringBuilder();
            varContent.Append("\""+varName+"\"" + ":");
            if (typeName.Contains("[]"))
            {
                varContent.Append("[");
                string[] constStrs = content.Split('#');
                for (int i = 0; i < constStrs.Length; i++)
                {
                    if (typeName.Contains("number"))
                    {
                        varContent.Append(constStrs[i]);
                    }
                    else
                    {
                        varContent.Append("\"" + constStrs[i] + "\"");
                    }
                    if (i != constStrs.Length - 1)
                    {
                        varContent.Append(",");
                    }
                }
                varContent.Append("]");
            }
            else
            {
                if (typeName.Contains("number"))
                {
                    varContent.Append(content);
                }
                else
                {
                    varContent.Append("\"" + content + "\"");
                }
            }
            return varContent.ToString();
        }
        public void ExportCode()
        {

        }
    }
}
